package com.example.demo;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.demo.Bean.Show;
import com.example.demo.Bean.Weather;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class RegionActivity extends Activity {

    private String name;
    private String url;
    private Show mShow = new Show();
    private TextView mRegion;
    private TextView mTemperature;
    private TextView mOther;
    private TextView mContent;
    private TextView mTips;
    private TextView mDate;
    private ImageView mBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_region);
        name = getIntent().getStringExtra("name");
        url = "https://api.seniverse.com/v3/weather/now.json?key=SG0z9GwELWsxcTX31&location=" + name;
        moveStatusbar();
        initView();
        sendRequestWithOkHttp();
        while(mShow.getmWeather() == null || !mShow.getmWeather().getResults().get(0).getLocation().getName().equals(name));
        mRegion.setText(name+"市");
        mTemperature.setText(mShow.getmWeather().getResults().get(0).getNow().getTemperature()+"℃");
        mOther.setText(mShow.getOther());
        mContent.setText(Html.fromHtml(mShow.getShow()));
        mTips.setText(mShow.getTips());
        mDate.setText(mShow.getDate());
    }

    private void moveStatusbar() {
        if(Build.VERSION.SDK_INT >= 21) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
        }
    }

    private void initView() {
        mRegion = (TextView) findViewById(R.id.mregion);
        mTemperature = (TextView) findViewById(R.id.temperature);
        mOther = (TextView) findViewById(R.id.other);
        mContent = (TextView) findViewById(R.id.mcontent);
        mTips = (TextView) findViewById(R.id.tips);
        mDate = (TextView) findViewById(R.id.mdate);
        mBack = (ImageView) findViewById(R.id.mback);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void sendRequestWithOkHttp() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder().url(url).build();
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    parseJsonWithGson(responseData);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void parseJsonWithGson(String jsonData) {
        Gson gson = new Gson();
        mShow.setmWeather(gson.fromJson(jsonData, Weather.class));
    }

}